﻿using MaterialManagement.API.Application.Command.ERP;
using MaterialManagement.Domain.ERP;
using MaterialManagement.ErrorCode;
using MaterialManagement.Infrastructure.Interface;
using MediatR;

namespace MaterialManagement.API.Application.Handler.ERP
{
    public class GetIdMaterialInfoHandler : IRequestHandler<GetIdMaterialInfoCommand, APIResult<MaterialInfoModel>>
    {
        private readonly IBaseRepository<MaterialInfoModel> materialInfoRepo;

        public GetIdMaterialInfoHandler(IBaseRepository<MaterialInfoModel> materialInfoRepo)
        {
            this.materialInfoRepo = materialInfoRepo;
        }
        public Task<APIResult<MaterialInfoModel>> Handle(GetIdMaterialInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<MaterialInfoModel> result = new APIResult<MaterialInfoModel>();
            var res = materialInfoRepo.GetAll().FirstOrDefault(x=>x.MaterialCode == request.MaterialCode);
            if (res == null)
            {
                result.Code = APIEnum.Fail;
                result.Message = "找不到此编号";
                return Task.FromResult(result);
            }
            result.Data = res;
            result.Message = "查询物料信息成功";
            result.Code = APIEnum.Ok;
            return Task.FromResult(result);
        }
    }
}
